package com.bskj.sj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.bean.model.ParkingShop;
import com.bskj.common.constants.CommonConstatns;
import com.bskj.common.dao.BmsUserDao;
import com.bskj.common.dao.ParkingShopDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.OrderEnum;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.BmsUserTypeEnum;
import com.bskj.sj.service.IParkingShopService;

/**
 * Created by mocaijin on 21/01/2018.
 */
@Service
public class ParkingShopServiceImpl extends BaseServiceImpl<ParkingShop> implements IParkingShopService {

    @Autowired
    private ParkingShopDao parkingShopDao;

    @Autowired
    private BmsUserDao bmsUserDao;

    @Override
    public BaseDao<ParkingShop> getBaseDao() {
        return parkingShopDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ParkingShop parkingShop) throws Exception {
        parkingShopDao.insert(parkingShop);
        BmsUser bmsUser = null;
        if (parkingShop != null && parkingShop.getBmsUser() != null) {
        	bmsUser = parkingShop.getBmsUser();
        } else {
        	bmsUser = new BmsUser();
            parkingShop.setBmsUser(bmsUser);
        }
        bmsUser.setCustId(parkingShop.getParkingLotCustomerId());
        bmsUser.setShopId(parkingShop.getId());
        //'1:停车场id，2:商户id'
        bmsUser.setType(BmsUserTypeEnum.parking_shop_user.getKey());
        bmsUser.setEntNo(parkingShop.getPhone());
        bmsUser.setPassword("shop123456");
        bmsUserDao.insert(parkingShop.getBmsUser());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(BmsUser bmsUser) {
    	bmsUserDao.insert(bmsUser);
    }

    @Override
    public BmsUser getByCustId(int custId) throws Exception {

        FindRequestEntity findRequestEntity = new FindRequestEntity();
        findRequestEntity.addWhere("cust_id", WhereEnum.EQ, custId);
        //收费方式（0-未支付 1-现金 2-平台代扣）
        List<BmsUser> authInfoList = bmsUserDao.find(findRequestEntity);
        if (authInfoList != null && authInfoList.size() >= 1) {
            return authInfoList.get(0);
        }
        return null;
    }

    @Override
    public List<ParkingShop> get(ParkingShop parkingShop) throws Exception {

        FindRequestEntity findRequestEntity = new FindRequestEntity();
        if (parkingShop.getId() != null) {
            findRequestEntity.addWhere("id", WhereEnum.EQ, parkingShop.getId());
        }
        if (parkingShop.getParkingLotCustomerId() != null) {
            findRequestEntity.addWhere("parking_lot_customer_id", WhereEnum.EQ, parkingShop.getParkingLotCustomerId());
        }
        if (parkingShop.getStatus() != null) {
            findRequestEntity.addWhere("status", WhereEnum.EQ, parkingShop.getStatus());
        }
        if (parkingShop.getType() != null) {
            findRequestEntity.addWhere("type", WhereEnum.EQ, parkingShop.getType());
        }
        if (parkingShop.getName() != null) {
            findRequestEntity.addLike("name", parkingShop.getName());
        }


        return parkingShopDao.find(findRequestEntity);
    }

    @Override
    public List<ParkingShop> getParkshops(ParkingShop parkingShop, Integer page, Integer pageSize) throws Exception {
        if (page == null || page == 0) {
            page = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CommonConstatns.DEFAULT_PAGE_SZIE;
        }
        FindRequestEntity findRequestEntity = new FindRequestEntity();
        if (parkingShop != null) {
            if (parkingShop.getId() != null) {
                findRequestEntity.addWhere("id", WhereEnum.EQ, parkingShop.getId());
            }
            if (parkingShop.getParkingLotCustomerId() != null) {
                findRequestEntity.addWhere("parking_lot_customer_id", WhereEnum.EQ, parkingShop.getParkingLotCustomerId());
            }
            if (parkingShop.getName() != null) {
                findRequestEntity.addLike("name", parkingShop.getName());
            }
            if (parkingShop.getStatus() != null) {
                findRequestEntity.addWhere("status", WhereEnum.EQ, parkingShop.getStatus());
            }
        }
        findRequestEntity.addOrder("create_time", OrderEnum.DESC);
        findRequestEntity.setPage(page, pageSize);

        List<ParkingShop> parkingShopList = find(findRequestEntity);
        return parkingShopList;
    }


}
